package com.qiangesoft.mybatisflex.utils;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.qiangesoft.mybatisflex.base.BaseEntity;
import com.zaxxer.hikari.HikariDataSource;

/**
 * 代码生成工具类
 */
public class CodegenUtil {

    public static void main(String[] args) {
        // 配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/db_mybatisflex?useInformationSchema=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        // 创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        // 通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        // 生成代码
        generator.generate();
    }

    /**
     * 常规风格
     */
    public static GlobalConfig createGlobalConfigUseStyle1() {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 设置路径及根包
        globalConfig.setSourceDir("D:/work/code/boot-business/boot-business-mybatisflex/src/main/java");
        globalConfig.setBasePackage("com.qiangesoft.mybatisflex");
        globalConfig.setMapperXmlPath("D:/work/code/boot-business/boot-business-mybatisflex/src/main/resources/mapper");

        // 设置生成策略
//        globalConfig.setTablePrefix("tb_");
        globalConfig.setGenerateTable("sys_user", "sys_dept");
        globalConfig.setUnGenerateTable("tb_test");
        globalConfig.setLogicDeleteColumn("deleted");
        globalConfig.setVersionColumn("version");

        // 设置 javadoc
        globalConfig.setAuthor("qiangesoft");
        globalConfig.setSince("1.0.0");

        // 设置生成 entity
        globalConfig.setEntityWithSwagger(true);
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        globalConfig.setEntitySuperClass(BaseEntity.class);
        globalConfig.setEntityJdkVersion(11);

        // 设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.setMapperAnnotation(true);

        // 设置生成 mapper.xml
        globalConfig.setMapperXmlGenerateEnable(true);

        // 设置生成 service
        globalConfig.setServiceGenerateEnable(true);

        // 设置生成 serviceImpl
        globalConfig.setServiceImplGenerateEnable(true);

        // 设置生成 controller
        globalConfig.setControllerGenerateEnable(true);
        globalConfig.setControllerRestStyle(true);
//        globalConfig.setControllerRequestMappingPrefix("sys");

        // 单独配置某个列
//        ColumnConfig columnConfig1 = new ColumnConfig();
//        columnConfig1.setColumnName("create_time");
//        columnConfig1.setOnInsertValue("now()");
//
//        ColumnConfig columnConfig2 = new ColumnConfig();
//        columnConfig2.setColumnName("update_time");
//        columnConfig2.setOnUpdateValue("now()");
//
//        globalConfig.setColumnConfig(columnConfig1);
//        globalConfig.setColumnConfig(columnConfig2);

        // 指定表
        // globalConfig.setColumnConfig("tb_account", columnConfig);

        return globalConfig;
    }

    /**
     * 链式风格
     */
    public static GlobalConfig createGlobalConfigUseStyle2() {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 设置路径及根包
        globalConfig.getPackageConfig()
                .setSourceDir("D:/work/code/boot-business/boot-business-mybatisflex/src/main/java")
                .setBasePackage("com.qiangesoft.mybatisflex")
                .setMapperXmlPath("D:/work/code/boot-business/boot-business-mybatisflex/src/main/resources/mapper");

        // 设置生成策略
        globalConfig.getStrategyConfig()
//                .setTablePrefix("sys_")
                .setGenerateTable("sys_user", "sys_dept")
                .setUnGenerateTable("tb_test")
                .setIgnoreColumns("test")
                .setLogicDeleteColumn("deleted")
                .setVersionColumn("version");

        // 设置 javadoc
        globalConfig.getJavadocConfig()
                .setAuthor("admin")
                .setSince("1.0.0");

        // 设置生成 entity
        globalConfig.enableEntity()
                .setWithSwagger(true)
                .setWithLombok(true)
                .setAlwaysGenColumnAnnotation(true)
                .setSuperClass(BaseEntity.class)
                .setJdkVersion(11);

        // 设置生成 mapper
        globalConfig.enableMapper()
                .setMapperAnnotation(true);

        // 配置生成 mapper.xml
        globalConfig.enableMapperXml();

        // 设置生成 service
        globalConfig.enableService();

        // 设置生成 serviceImpl
        globalConfig.enableServiceImpl();

        // 设置生成 controller
        globalConfig.enableController()
                .setRestStyle(true)
                .setRequestMappingPrefix("sys");

        // 单独配置某个列
//        ColumnConfig columnConfig1 = new ColumnConfig();
//        columnConfig1.setColumnName("create_time");
//        columnConfig1.setOnInsertValue("now()");
//
//        ColumnConfig columnConfig2 = new ColumnConfig();
//        columnConfig2.setColumnName("update_time");
//        columnConfig2.setOnUpdateValue("now()");
//
//        globalConfig.getStrategyConfig()
//                .setColumnConfig(columnConfig1)
//                .setColumnConfig(columnConfig2);

        return globalConfig;
    }

}